import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public String select(Map<String, String> db_info, String sql){
        String db_url = db_info.get("url");
        String db_driver = db_info.get("driver");
        String db_user = db_info.get("user");
        String db_pass = db_info.get("pass");
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(db_driver);
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            return this.dbResultToJson(rs);
        } catch(Exception e){
            e.printStackTrace();
        } finally{
            try{
                if(stmt != null) stmt.close();
            } catch(SQLException se2){
                se2.printStackTrace();
            }
            try{
                if(conn != null) conn.close();
            } catch(SQLException se) {
                se.printStackTrace();
            }
        }
        return null;
    }
    protected String dbResultToJson(ResultSet rs) throws SQLException{
        ResultSetMetaData rsMetaData = rs.getMetaData();
        int numberOfColumns = rsMetaData.getColumnCount();
        String result = "[";
        int count = 0;
        while(rs.next()){
            if(count > 0) result += ",";
			result += "{";
            for (int i = 1; i < numberOfColumns + 1; i++) {
                if(i > 1) result += ",";
                String fieldName = rsMetaData.getColumnName(i);
				result += "\"" + fieldName + "\":";
				String typeName = rsMetaData.getColumnTypeName(i);
				if (typeName.equals("INT")) {
				    int cont = rs.getInt(fieldName);
				    String cont_str = rs.getString(fieldName);
				    if(0 == cont && null == cont_str) result += "null";
				    else result += cont;
				} else if (typeName.equals("FLOAT")) {
				    float cont = rs.getFloat(fieldName);
				    String cont_str = rs.getString(fieldName);
				    if(0.0 == cont && null == cont_str) result += "null";
				    else result += cont;
				} else {
                    String cont = rs.getString(fieldName);
                    if(null == cont) result += "null";
				    else result += "\"" + cont + "\"";
				}
            }
		result += "}";
		count++;
        }
        return result + "]";
    }
    public String execsqls(Map<String, String> db_info, String[] sqls, boolean rollback) throws SQLException {
        String db_url = db_info.get("url");
        String db_driver = db_info.get("driver");
        String db_user = db_info.get("user");
        String db_pass = db_info.get("pass");
        Connection conn = null;
        Statement stmt = null;
        String result = "{";
        try {
            Class.forName(db_driver);
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
            stmt = conn.createStatement();
            conn.setAutoCommit(false);
            int count = 0;
            for (String sql : sqls) {
                count += stmt.executeUpdate(sql);
            }
            conn.commit();
            result += "\"status\": true, \"rows\": " + count;
        } catch(Exception e){
            result += "\"status\": false, \"error\": \"" + e.getMessage() + "\"";
            if(true == rollback){
                conn.rollback();
            } else {
                conn.commit();
            }
        } finally{
            try{
                if(stmt != null) stmt.close();
            } catch(SQLException se2){
                se2.printStackTrace();
            }
            try{
                if(conn != null) conn.close();
            } catch(SQLException se) {
                se.printStackTrace();
            }
        }
        return result + "}";
    }
    public static void main(String[] args) throws Exception{
        System.out.println("welcome to db opr for java");
    }
}
